using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using DailyReport.Core.Bussines.IManagers;
using DailyReport.Core.Data.Dao;
using DailyReport.Core.Data.Dao.DaoFactory;
using DailyReport.Core.Data.Model;
using log4net;

namespace DailyReport.Core.Bussines
{
    public class TaskManager:ITaskManager<Task>
    {
        private ILog log = LogManager.GetLogger("DailyReportManager");
        
        public void createTask(int userId, string title, string description)
        {
            Task t = new Task();
            t.Title = title;
            t.Description = description;

            if (userId > 0)
            {
                IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
                t.User = userDao.GetById(userId, false);
            }
            ITaskDao taskDao = (ITaskDao)DaoFactory.i.getDaoInstance("TaskDao");
            taskDao.Save(t);
        }

        public void updateTask(int taskId, int userId, string title, string description)
        {
            ITaskDao taskDao = (ITaskDao)DaoFactory.i.getDaoInstance("TaskDao");
            Task t = taskDao.GetById(taskId,false);
            t.Title = title;
            t.Description = description;

            if (userId > 0)
            {
                IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
                t.User = userDao.GetById(userId, false);
            }
            taskDao.SaveOrUpdate(t);
        }

        public void deleteTask(int taskId)
        {
            ITaskDao taskDao = (ITaskDao)DaoFactory.i.getDaoInstance("TaskDao");
            Task t = taskDao.GetById(taskId, false);
            taskDao.Delete(t);
        }

        public Task loadTask(int taskId)
        {
            ITaskDao taskDao = (ITaskDao)DaoFactory.i.getDaoInstance("TaskDao");
            Task t = taskDao.GetById(taskId, false);
            return (t);
        }
                
        public User loadUser(int userId)
        {
            IUserDao userDao = (IUserDao)DaoFactory.i.getDaoInstance("UserDao");
            User u = userDao.GetById(userId, true);
            return (u);
        }
        
        public IList GetInstanceList(string sortExpression, int startRowIndex, int maxRows)
        {
            log.Warn("****#TASKMANAGER GetInstanceList METHOD");
            ITaskDao taskDao = (ITaskDao)DaoFactory.i.getDaoInstance("TaskDao");
            IList res = taskDao.getInstancesList(sortExpression, maxRows, startRowIndex);
            log.Warn("****#TASK CARGADOS: "+res.Count);
            return (res);
        }

        public int CountInstances()
        {
            log.Warn("****#TASKMANAGER CountInstances METHOD");
            ITaskDao taskDao = (ITaskDao)DaoFactory.i.getDaoInstance("TaskDao");
            int res = taskDao.getInstancesCount();
            log.Warn("****#TASKs : " + res);
            return (res);
        }
    }
}
